<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/1/24
 * Time: 16:35
 */
namespace app\mec\controller;
use think\Db;
class Business extends Base {

    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //手机号
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['s_name|s_phone'] = ['like', "%" . $params['key'] . "%"];
            }
            $where['s_status'] = array('IN',array(2,4));
            if(isset($params['user_face']) && $params['user_face'] != '')
            {
                $where['s_status'] = $params['user_face'];
            }
            $dl_num = Db::table('up_dl')->where('dl_id',session('uid1'))->value('dl_num');
            $where['s_id'] = array('IN',$dl_num);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shops')->where($where)->count();// 获取总条数
            $lists = Db::name('shops')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('s_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?s_id')){
            session('s_id',null);
        }
        if(session('?tu_id')){
            session('tu_id',null);
        }
        return $this->fetch();
    }
    //基本信息
    public function basic(){

            $id = input('param.id');
            $where['s_id'] = $id;
            $lists = Db::name('shops')
                ->where($where)
                ->find();
        if($lists['s_status']==2){
            $lists['status_name'] = '认证完成';
        }elseif ($lists['s_status']==4){
            $lists['status_name'] = '未认证';
        }
        $this->assign('info',$lists);
        return $this->fetch();
    }

    //账户信息

    public function account(){
        if(request()->isAjax()){
            $params = input('param.');
            if(isset($params['shop_apply_status']) && $params['shop_apply_status'] != '')
            {
                $where['shop_apply_status'] = $params['shop_apply_status'];
            }
            $id = input('param.id');
            $where['shop_id'] = $id;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shop_apply')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('shop_apply')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?s_id')){
            session('s_id',input('id'));
        }
        return $this->fetch();
    }

    //任务信息

    public  function task(){
        if(request()->isAjax()){
            $params = input('param.');
            if(isset($params['task_type']) && $params['task_type'] != '')
            {
                $where['task_type'] = $params['task_type'];
            }
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['task_name'] = ['like', "%" . $params['key'] . "%"];
            }
            $id = input('param.id');
            $where['task_s_id'] = $id;
            $where['task_status'] = array('NEQ',5);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('task')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('task')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['task_status']==1){
                    $vo['task_status_name']='上架';
                }elseif ($vo['task_status']==2){
                    $vo['task_status_name']='上架';
                }elseif ($vo['task_status']==3){
                    $vo['task_status_name']='上架';
                }elseif ($vo['task_status']==4){
                    $vo['task_status_name']='下架';
                }
                if($vo['task_active']==1){
                    $vo['task_active_name'] ='普通任务';
                }elseif ($vo['task_active']==2){
                    $vo['task_active_name'] ='活动任务';
                }elseif ($vo['task_active']==3){
                    $vo['task_active_name'] ='达人任务';
                }
                $vo['wcl'] = ($vo['task_done_num']/$vo['task_num'])*100 . '%';
                if($vo['task_sheng'] == 0){
                    $vo['address'] = '全国';
                }else{
                    $city = Db::name('area')
                        ->whereIn('district_id',$vo['task_sheng'])
                        ->column ('district');
                    $vo['address'] = implode (',',$city);
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?s_id')){
            session('s_id',input('id'));
        }
        return $this->fetch();
    }
    //任务的基本详情信息
    public function essential(){
        $id = input('param.id');
        $where['task_id'] = $id;
        $info = Db::table('up_task')->where($where)->find();
        if($info['task_type']==1){
            $info['task_type_name'] = '下载';
        }elseif ($info['task_type']==2){
            $info['task_type_name'] = '求赞求粉';
        }elseif ($info['task_type']==3){
            $info['task_type_name'] = '问卷';
        }elseif ($info['task_type']==4){
            $info['task_type_name'] = '投票';
        }elseif ($info['task_type']==5){
            $info['task_type_name'] = '视频';
        }elseif ($info['task_type']==6){
            $info['task_type_name'] = '分享';
        }
        $quyu = '';
        if($info['task_sheng'] == 0){
            $quyu = '全国';
        }else{
            $quyu .= Db::table('up_area')
                ->where('district_id',$info['task_sheng'])
                ->value('district');
            if($info['task_shi'] == 0){
                $quyu .= '>全部';
            }else{
                $quyu .= '>' . Db::table('up_area')
                        ->where('district_id',$info['task_shi'])
                        ->value('district');
                if($info['task_sheng'] != 0 && $info['task_shi'] != 0){
                    $qus = Db::table('up_area')
                        ->where('district_id','in',$info['task_qu'])
                        ->column('district');
                    $quyu .= '>' . implode($qus,' ');
                }else{
                    $quyu .= '>全部';
                }
            }
        }
        $info['address'] = $quyu;
        $info['wcl'] = ($info['task_done_num']/$info['task_num'])*100 . '%';
        $this->assign('info',$info);
        return $this->fetch();
    }
    //领取信息
    public function receive(){
        if(request()->isAjax()){
            $where['a.tu_task_id'] = input('param.id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('task_user')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.tu_user_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('task_user')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.tu_user_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_image')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);

        }
        if(!session('?tu_id')){
            session('tu_id',input('id'));
        }
        return $this->fetch();
    }
    //冻结
    public function frozen(){
        $id = input('id');
        $res = Db::table('up_shops')->where('s_id', $id)->setField('s_sta', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    //解冻
    public function thaw(){
        $id = input('id');
        $res = Db::table('up_shops')->where('s_id', $id)->setField('s_sta', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    //充值
    public function recharge(){
        if(request()->isPost()){
            $id = input('param.id');
            $s_money = input('param.s_money');
            $info = Db::table('up_dl')->where('dl_id',session('uid1'))->find();
            if($s_money>$info['dl_balance']){
                return json(['code' => 100, 'msg' => '代理商余额不足,无法充值']);
            }else{
                Db::table('up_dl')->where('dl_id',session('uid1'))->setDec('dl_balance',$s_money);
            }
            Db::startTrans();
            //查询折合人民币
            $ptb = Db::table('up_about_us')->where('about_id',1)->value('ptb');
            $res = Db::table('up_shops')->where('s_id',$id)->setInc('s_money', $s_money*$ptb);
            $order_number = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);//订单编号
            //插入商家收支明细
            $data['shop_apply_status'] = 2;
            $data['shop_apply_type'] = 7;//1发布任务扣款 2总后台扣款 3配置达人 4微信充值 5支付宝充值 6平台充值
            $data['shop_apply_money'] = $s_money;
            $data['shop_apply_ptb'] = $s_money*$ptb;
            $data['shop_time'] = date('Y-m-d H:i:s',time());
            $data['shop_id'] = $id;
            $data['shop_title'] = "代理商充值".$s_money.'元';
            $data['shop_apply_number'] = $order_number;
            $aa = Db::table('up_shop_apply')->insertGetId($data);
            $data1['revenue_number'] = $order_number;
            $data1['revenue_time'] = date('Y-m-d H:i:s',time());
            $data1['revenue_type'] = 1;
            $data1['revenue_gold'] = $s_money*$ptb;
            $data1['revenue_money'] = $s_money;
            $data1['revenue_mode'] = 6;
            $aa1 = Db::table('up_revenue')->insertGetId($data1);
            allInNotice("充值","平台充值".$s_money.'元',3,$id);
            if($res&&$aa&&$aa1){
                Db::commit();
                $insert = [
                    'less_dl_id' => session('uid1'),
                    'less_s_id' => $id,
                    'less_money' => $s_money,
                    'less_ptb' => $s_money*10,
                    'less_time' => date('Y-m-d H:i:s')
                ];
                Db::table('up_dl_less')->insert($insert);
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                Db::rollback();//回滚
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $this->assign('id',input('param.id'));
        return $this->fetch();
    }
    //扣款
    public function deduction(){
        if(request()->isPost()){
            $id = input('param.id');
            $s_money = input('param.s_money');
            $result = Db::table('up_shops')->where('s_id',$id)->find();
             if($result['s_money']<$s_money){
                 return json(['code' => 100, 'msg' => '用户余额不足,请先充值']);
             }
            //查询折合人民币
            $ptb = Db::table('up_about_us')->where('about_id',1)->value('ptb');

            $res = Db::table('up_shops')->where('s_id',$id)->setDec('s_money',$s_money*$ptb);
            $order_number = date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);//订单编号
            $data['shop_apply_status'] = 1;
            $data['shop_apply_type'] = 2;//1发布任务扣款 2总后台扣款 3配置达人 4微信充值 5支付宝充值 6平台充值
            $data['shop_apply_money'] = $s_money;
            $data['shop_apply_ptb'] = $s_money*$ptb;
            $data['shop_time'] = date('Y-m-d H:i:s',time());
            $data['shop_id'] = $id;
            $data['shop_title'] = "平台扣款".$s_money;
            $data['shop_apply_number'] = $order_number;
            $aa = Db::table('up_shop_apply')->insertGetId($data);
            allInNotice("扣款","平台扣款".$s_money.'元',3,$id);

            $data1['revenue_number'] = $order_number;
            $data1['revenue_time'] = date('Y-m-d H:i:s',time());
            $data1['revenue_type'] = 1;
            $data1['revenue_gold'] = $s_money*$ptb;
            $data1['revenue_money'] = $s_money;
            $data1['revenue_mode'] = 4;
            $aa1 = Db::table('up_revenue')->insertGetId($data1);
            if($res&&$aa&&$aa1){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $this->assign('id',input('param.id'));
        return $this->fetch();
    }

    public function num(){
        if(request()->isPost()){
            $id = input('param.id');
            $s_active_num = input('param.s_active_num');
            $res = Db::table('up_shops')->where('s_id',$id)->setField('s_active_num',$s_active_num);

            //查询折合人民币

            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $s_active_num = Db::table('up_shops')->where('s_id',input('param.id'))->value('s_active_num');
        $this->assign('id',input('param.id'));
        $this->assign('s_active_num',$s_active_num);
        return $this->fetch();
    }
}